//
//  MainFirst_MagazineList_PGCCell_WX.m
//  ZITHER
//
//  Created by weixin on 2018/10/15.
//  Copyright © 2018年 weixn. All rights reserved.
//

#import "MainFirst_MagazineList_PGCCell_WX.h"
#import "MainFirst_MagazineVideo_VC_WX.h"
#import "Ugc_HomePage_VC_V3.h"

@implementation MainFirst_MagazineList_PGCCell_WX

- (void)awakeFromNib {
    [super awakeFromNib];
    [self configComment];
    [self setUI];
    [self setMasonry];
}

-(void)setModel:(CoverCreateListModelSub *)model
{
    _model = model;
    
    if ([self.recommend isEqualToString:@"2"]) {
        NSString *str = [NSString stringWithFormat:@"%@%@",RootUrlImage,model.avatar];
        [self.userImage sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"ucg_img_loding"]];
        self.userName.text = model.nickName;
        self.time.text = [model.createTime convertCurrentTime];
    }
    //当recommend为1的时候显示系统样式
    

    self.userHead.model = model;
    if ([self.recommend isEqualToString:@"1"]) {
        
        self.time.hidden = YES;
        self.userName.hidden = YES;
        self.userImage.hidden = YES;
        self.userHead.hidden = NO;
        [self.userHead mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(60);
        }];
        [self.viewBorder mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(90);
            make.left.equalTo(self).offset(14);

        }];
        
    }else if([self.recommend isEqualToString:@"2"]){
        self.time.hidden = NO;
        self.userName.hidden = NO;
        self.userImage.hidden = NO;

        self.userHead.hidden = YES;
        
        [self.userHead mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
        
        [self.viewBorder mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(40);
            make.left.equalTo(self).offset(70);
        }];
  
    } else {
        
        self.time.hidden = YES;
        self.userName.hidden = YES;
        self.userImage.hidden = YES;
        self.userHead.hidden = YES;
        [self.userHead mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
        [self.viewBorder mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(0);
            make.left.equalTo(self).offset(14);
        }];
    }
    
    
    self.title.text = model.pgcTitle;
    if ([model.documentType isEqualToString:@"video"]) {
        [self.ImageV sd_setImageWithURL:[NSURL URLWithString:model.videoCoverImg] placeholderImage:[UIImage imageNamed:@"ucg_img_loding"]];
        self.play.hidden = NO;
    }else{
        [self.ImageV sd_setImage:model.coverImg];
        self.play.hidden = YES;
    }
    
    if (self.model.videoUrl.length>0) {
        self.ImageV.userInteractionEnabled = YES;
    }else{
        self.ImageV.userInteractionEnabled = NO;
    }
    
    if (model.isFirst) {
        self.iconFirst.hidden = NO;
    }else{
        self.iconFirst.hidden = YES;
    }

}

-(void)setMasonry{
    
    [self.userImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(10);
        make.left.equalTo(self).offset(20);
        make.width.height.mas_equalTo(40);
    }];
    
    //    self.userName.backgroundColor = [UIColor randomColor];
    [self.userName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(10);
        make.left.equalTo(self.userImage.mas_right).offset(10);
        make.height.mas_equalTo(15);
    }];
    
    //    self.time.backgroundColor = [UIColor randomColor];
    [self.time mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.userName.mas_bottom).offset(0);
        make.left.equalTo(self.userName.mas_right).offset(10);
        make.height.mas_equalTo(13);
        
    }];
    
    
    [self.userHead mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(0);
        make.left.right.equalTo(self).offset(0);
        make.height.mas_equalTo(60);
    }];
    
    [self.ImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.viewBorder.mas_top).offset(0);
        make.left.right.equalTo(self.viewBorder).offset(0);
        make.height.mas_equalTo(kScreenW/350.0*222);
    }];
    
    [self.title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.ImageV.mas_bottom).offset(12);
        make.left.equalTo(self.viewBorder).offset(14);
        make.right.equalTo(self.viewBorder).offset(-14);
        make.bottom.equalTo(self).offset(-45);
    }];
    
}

-(void)setUI{

    [self addSubview:self.userImage];
    [self addSubview:self.userName];
    [self addSubview:self.time];
    [self addSubview:self.title];
//    [self addSubview:self.ImageV];
    [self.viewBorder addSubview:self.self.ImageV];

//    self.title.backgroundColor = [UIColor randomColor];
//    self.ImageV.backgroundColor = [UIColor randomColor];
    self.title.backgroundColor = [UIColor clearColor];
    self.ImageV.backgroundColor = [UIColor clearColor];

    [self addSubview:self.play];
    [self.play mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.ImageV.mas_left).offset(10);
        make.bottom.equalTo(self.ImageV.mas_bottom).offset(-10);
        make.height.width.mas_equalTo(40);
    }];
    [self addSubview:self.userHead];

    if ([self.recommend isEqualToString:@"1"]) {
        [self.ImageV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.userHead.mas_bottom).offset(10);
        }];
        
    }else if([self.recommend isEqualToString:@"2"]){
        [self.ImageV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(0);
        }];
    }else{
        [self.ImageV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(0);
        }];
    }
    
     [self addSubview:self.iconFirst];
    
    [self.iconFirst mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-24);
        make.top.equalTo(self).offset(10);
        make.width.mas_equalTo(22);
        make.height.mas_equalTo(24);

    }];
}

-(void)configComment{
    [self setBorderWithHiddem:YES Color:[UIColor whiteColor] width:0.5 Radius:15];
    self.viewBorder.clipsToBounds = YES;
    self.viewBorder.backgroundColor = [UIColor whiteColor];

    self.backgroundColor = HexColor(0xf5f5f5);
}

- (BaseLab *)title{
    if (!_title) {
        _title = [[BaseLab alloc]initializeFrame:CGRectMake(0, 0, 1, 1) text:@"" font:20 textColor:[UIColor blackColor] isBold:YES numberOfLines:2];
        _title.font = [UIFont fontWithName:@"PingFangSC-Medium" size:20];
        

    }
    return _title;
}

- (BaseImageV *)ImageV{
    if (!_ImageV) {
        _ImageV = [[BaseImageV alloc]initializeFrame:CGRectMake(0, 0, 1, 1) contentMode:2];
        /** 添加点击事件 */
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        //videoUrl
        [[tap rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
            HomePageDetailModelpgcJson *model =[[HomePageDetailModelpgcJson alloc]init];
            model.src = self.model.videoUrl;
            MainFirst_MagazineVideo_VC_WX *vc = [[MainFirst_MagazineVideo_VC_WX alloc]init];
            vc.modelJson = model;
            [self.getNav pushViewController:vc animated:YES];
        }];
        _ImageV.userInteractionEnabled = YES;
        [_ImageV addGestureRecognizer:tap];
//        [_ImageV setCornerRadiusWithRadius:15 isMasks:YES];
    }
    return _ImageV;
}

-(BaseImageV *)play{
    if (!_play) {
        _play = [[BaseImageV alloc]initializeFrame:CGRectMake(0, 0, 1, 1) contentMode:2];
        _play.backgroundColor = [UIColor clearColor];
        _play.image = [UIImage imageNamed:@"视频_播放"];

        
    }
    return _play;
}

-(MainFirst_MagazineList_UserHeadCell_WX *)userHead{
    if (!_userHead) {
        _userHead = [[MainFirst_MagazineList_UserHeadCell_WX alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 65)];
    }
    return _userHead;
}


-(UIImageView *)userImage{
    if (!_userImage) {
        _userImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
        [_userImage setCornerRadiusWithRadius:20 isMasks:YES];
        _userImage.contentMode = 2;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        [[tap rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
            Ugc_HomePage_VC_V3 *vc = [[Ugc_HomePage_VC_V3 alloc]init];
            vc.userName = self.model.nickName;
            vc.userId = self.model.userId;
            vc.userAvatar =  self.model.avatar;
            [self.getNav pushViewController:vc animated:YES];
        }];
        [_userImage addGestureRecognizer:tap];
        _userImage.userInteractionEnabled = YES;
        
    }
    return _userImage;
}


-(QMUILabel *)userName{
    if (!_userName) {
        _userName = [[QMUILabel alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
        _userName.font = [UIFont systemFontOfSize:15];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        [[tap rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
            Ugc_HomePage_VC_V3 *vc = [[Ugc_HomePage_VC_V3 alloc]init];
            vc.userName = self.model.nickName;
            vc.userId = self.model.userId;
            vc.userAvatar =  self.model.avatar;
            [self.getNav pushViewController:vc animated:YES];
        }];
        [_userName addGestureRecognizer:tap];
        _userName.userInteractionEnabled = YES;
    }
    return _userName;
}

-(QMUILabel *)time{
    if (!_time) {
        _time = [[QMUILabel alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
        _time.font = [UIFont systemFontOfSize:13];
        _time.textColor = baseColorMedium;
    }
    return _time;
}

-(BaseImageV *)iconFirst{
    if (!_iconFirst) {
        _iconFirst = [[BaseImageV alloc]initializeFrame:CGRectMake(0, 0, 0, 0) contentMode:UIViewContentModeCenter];
        _iconFirst.backgroundColor = [UIColor whiteColor];
        [_iconFirst setCornerRadiusWithRadius:4 isMasks:YES];
        _iconFirst.image = [self scaleToSize:[UIImage imageNamed:@"动态_置顶"] size:CGSizeMake(15, 15)];
//        _iconFirst.image = [UIImage imageNamed:@"动态_置顶"];

    }
    return _iconFirst;
}
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)newsize{
    UIGraphicsBeginImageContext(newsize);
    [img drawInRect:CGRectMake(0, 0, newsize.width, newsize.height)];
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}


@end
